import _ from '../../../utils/under-dash';
import BaseXform from '../base-xform';

class SheetFormatPropertiesXform extends BaseXform {
    get tag() {
        return 'sheetFormatPr';
    }

    render(xmlStream, model) {
        if (model) {
            const attributes = {
                defaultRowHeight: model.defaultRowHeight,
                outlineLevelRow: model.outlineLevelRow,
                outlineLevelCol: model.outlineLevelCol,
                'x14ac:dyDescent': model.dyDescent,
            };
            if (model.defaultColWidth) {
                attributes.defaultColWidth = model.defaultColWidth;
            }

            // default value for 'defaultRowHeight' is 15, this should not be 'custom'
            if (!model.defaultRowHeight || model.defaultRowHeight !== 15) {
                attributes.customHeight = '1';
            }

            if (_.some(attributes, value => value !== undefined)) {
                xmlStream.leafNode('sheetFormatPr', attributes);
            }
        }
    }

    parseXmlObject(node) {
        if (node._name === 'sheetFormatPr') {
            this.model = {
                defaultRowHeight: parseFloat(node._attributes.defaultRowHeight || '0'),
                dyDescent: parseFloat(node._attributes['x14ac:dyDescent'] || '0'),
                outlineLevelRow: parseInt(node._attributes.outlineLevelRow || '0', 10),
                outlineLevelCol: parseInt(node._attributes.outlineLevelCol || '0', 10),
            };
            if (node._attributes.defaultColWidth) {
                this.model.defaultColWidth = parseFloat(node._attributes.defaultColWidth);
            }
            return true;
        }
        return false;
    }

    parseOpen(node) {
        if (node.name === 'sheetFormatPr') {
            this.model = {
                defaultRowHeight: parseFloat(node.attributes.defaultRowHeight || '0'),
                dyDescent: parseFloat(node.attributes['x14ac:dyDescent'] || '0'),
                outlineLevelRow: parseInt(node.attributes.outlineLevelRow || '0', 10),
                outlineLevelCol: parseInt(node.attributes.outlineLevelCol || '0', 10),
            };
            if (node.attributes.defaultColWidth) {
                this.model.defaultColWidth = parseFloat(node.attributes.defaultColWidth);
            }
            return true;
        }
        return false;
    }

    parseText() {
    }

    parseClose() {
        return false;
    }
}

export default SheetFormatPropertiesXform;
